package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.mapper.MemberMapper;
import com.ujiuye.pojo.Member;

import com.ujiuye.service.IMemberService;
import com.ujiuye.utils.PageResult;
import com.ujiuye.utils.QueryPageBean;
import com.ujiuye.vo.MemberVo;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Resource
    private MemberMapper memberMapper;

    @Override
    public PageResult listPage(QueryPageBean queryPageBean) {
        //创建分页对象
        Page<Member> page =
                new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        //创建条件构造器
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        //判断查询条件是否为空
        if (queryString!=null && queryString.length()>0){
            queryWrapper.eq("name",queryString)
                    .or().eq("phonenumber",queryString);
        }
        //查性别
        if (queryPageBean.getQuerySex()!=null && !queryPageBean.getQuerySex().equals("不限")){
            queryWrapper.eq("sex",queryPageBean.getQuerySex());
        }
        //查注册时间
        if (queryPageBean.getQueryReg()!=null && !queryPageBean.getQueryReg().equals("不限")){
            String beginTime = queryPageBean.getQueryReg() + "-01-01";
            String endTime = queryPageBean.getQueryReg() + "-12-31";
            queryWrapper.between("regTime",beginTime,endTime);
        }
        //查年龄
        if (queryPageBean.getQueryAge()!=null && !queryPageBean.getQueryAge().equals("不限")){
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH)+1;
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            if (queryPageBean.getQueryAge().contains("-")){
                String[] split = queryPageBean.getQueryAge().split("-");
                String endTime = (year-Integer.parseInt(split[0]))+"-"+month+"-"+day;
                String startTime = (year-Integer.parseInt(split[1]))+"-"+month+"-"+day;
                queryWrapper.between("birthday",startTime,endTime);
            }else { //60岁以上
                String time = (year-60)+"-"+month+"-"+day;
                queryWrapper.lt("birthday",time);
            }
        }
        //执行查询
        Page<Member> memberPage = memberMapper.selectPage(page, queryWrapper);
        //创建返回结果对象
        return new PageResult(memberPage.getTotal(),memberPage.getRecords());
    }

    @Override
    public List<Object> getAllYear() {
        //创建条件构造器
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("min(regTime) as regTime");
        List<Member> members = memberMapper.selectList(queryWrapper);//查询数据库最早的注册时间

        int sysYear = members.get(0).getRegtime().getYear();
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);//求当前时间

        List<Object> list = new ArrayList<>();
        list.add("不限");
        for (int i=sysYear;i<=currentYear;i++){
            list.add(i);
        }
        return list;
    }

    @Override
    public List<Member> listByCurrentPage(Integer currentPage, Integer pageSize) {
        //创建分页对象
        Page<Member> page = new Page<>(currentPage,pageSize);
        //执行查询
        Page<Member> memberPage = memberMapper.selectPage(page, null);
        List<Member> list = memberPage.getRecords();
        return list;
    }

    @Override
    public MemberVo listMemberVo() {
        //过去一年内每个月会员数量
        List<String> monthList = new ArrayList<>();//保存月份
        List<Integer> memberList = new ArrayList<>();//保存会员数量
        Calendar calendar = Calendar.getInstance();//当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        calendar.add(Calendar.MONTH,-13);//往前推12个月
        for (int i=1;i<=13;i++){
            calendar.add(Calendar.MONTH,1);//每次循环加一个月
            String dateString = sdf.format(calendar.getTime());
            monthList.add(dateString);
            //求每个月份会员数量
            String qDate = dateString+"-31";
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.le("regTime",qDate);
            Integer count = memberMapper.selectCount(queryWrapper);
            memberList.add(count);
        }
        return new MemberVo(monthList,memberList);
    }


}
